package com.xl333.thymeleaf;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import java.util.Collections;

/**
 * @author 七月的狮子
 * @date 2020/5/19
 */
public class ThymeleafUtil {

    private static ThymeleafUtil instance = null;

    private ThymeleafUtil(){

    }

    public static ThymeleafUtil getInstance(){
        if(instance == null){
            instance = new ThymeleafUtil();
        }
        return instance;
    }

    public TemplateEngine getTemplateEngine(){
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.addTemplateResolver(templateResolver());
        return templateEngine;
    }

    /**
     * 配置模板的位置/前缀/后缀/模式等
     * @return
     */
    private ITemplateResolver templateResolver(){
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setOrder(1);
        templateResolver.setResolvablePatterns(Collections.singleton("/*"));    //需匹配模板名字
        templateResolver.setPrefix("/resource");
        templateResolver.setSuffix(".txt");
        templateResolver.setTemplateMode(TemplateMode.TEXT);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

}
